package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品管理相关接口")
@RequestMapping("/admin/dish")
@RestController("adminDishController")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO 菜品分页查询条件
     * @return 分页查询结果
     */
    @ApiOperation(value = "菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> getDishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.getDishPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增菜品
     * @param dishDTO 菜品信息
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    @Transactional // 开启事物
    public Result addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDish(dishDTO);
        return Result.success();
    }

    /**
     * 根据 ID 查询菜品
     * @param id 菜品id
     * @return 菜品信息以及关联的口味信息
     */
    @ApiOperation(value = "根据 ID 查询菜品")
    @ApiImplicitParam(name = "id", value = "菜品id", required = true, paramType = "path", dataType = "long")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        return Result.success(dishService.getDishById(id));
    }

    /**
     * 更新菜品
     * @param dishDTO 菜品信息
     */
    @ApiOperation(value = "更新菜品")
    @PutMapping
    @Transactional
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 更新菜品状态
     * @param status 状态
     * @param id 菜品id
     */
    @ApiOperation(value = "更新菜品状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态（0 停售 1 起售）", required = true, paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "id", value = "菜品id", required = true, paramType = "query", dataType = "long")
    })
    @PostMapping("/status/{status}")
    public Result updateDishStatus(@PathVariable Integer status, @RequestParam Long id) {
        dishService.updateDishStatus(status, id);
        return Result.success();
    }

    /**
     * 删除菜品
     * @param ids 菜品id集合
     */
    @ApiOperation(value = "删除菜品")
    @ApiImplicitParam(name = "ids", value = "菜品id集合", required = true, paramType = "query", dataType = "List<Long>", example = "1,2,3")
    @DeleteMapping
    @Transactional
    public Result deleteDish(@RequestParam List<Long> ids) {
        dishService.deleteDish(ids);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId 分类id
     * @return 菜品列表
     */
    @ApiOperation(value = "根据分类id查询菜品")
    @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, example = "1", paramType = "query", dataType = "long")
    @GetMapping("/list")
    public Result<List<DishVO>> getDishListByCategoryId(@RequestParam Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE); // 查询起售中的菜品
        List<DishVO> dishList = dishService.getDishListWithFlavor(dish);
        return Result.success(dishList);
    }

}
